#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);
static void my_avi_complete_cb(lv_obj_t* avi, void *user_data);

static lv_obj_t *avi_2;          //
static lv_obj_t *image_close;          //
static uint8_t flag_start_play_avi = 0;
static lv_obj_t *tc_area_back;          //
static lv_obj_t *bar_indic_voice;          //
static lv_obj_t *bar_indic_bl;          //




uint8_t flag_disp_bar = 0;
uint8_t flag_one_stop_trig = 0;

static lv_obj_t *image_video;          //视频教程子界面

static lv_obj_t *touch_area_avi_1;          //
static lv_obj_t *touch_area_avi_2;          //
volatile uint8_t g_flag_video_delay_play = 0; //特殊处理 视频播报前会重复播放开始一段语音 延迟播放处理



static uint16_t video_index = 0;
static const char *video_str_table2[3] =
{
    "S:sd:video_1.avi",
    "S:sd:video_2.avi",
};
/*
 * 界面回调函数
 */
static void screen007_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    static uint16_t time_cnt = 0;
    static uint16_t flag_disp_bar_onece = 0;
    static uint16_t fil_cnt = 0;
    static uint16_t time_cnt2 = 0;
    static uint16_t time_cnt3 = 0;

    screen_flush_pro();
    if (g_flag_video_delay_play == 1 && flag_start_play_avi == 1)
    {
        time_cnt3++;
        if (time_cnt3 >= 300 / 10)
        {
            g_flag_video_delay_play = 0;
            time_cnt3 = 0;
            my_dac_open();
        }
    }
    else
    {
        time_cnt3 = 0;
    }
    
    if (flag_one_stop_trig == 1 && flag_start_play_avi == 0)
    {
        time_cnt2++;
        if (time_cnt2 >= 300 / 10)
        {
            flag_one_stop_trig = 0;
            time_cnt2 = 0;
            lv_avi_stop(avi_2);
        }
    }
    else
    {
        time_cnt2 = 0;
    }
    if (flag_disp_bar == 1)
    {
        flag_disp_bar = 0;
        flag_disp_bar_onece = 1;
        time_cnt = 0;
    }
    if (flag_disp_bar_onece == 1)
    {
        time_cnt++;
        if (time_cnt > 500 / 10)
        {
            time_cnt = 0;
            flag_disp_bar_onece = 0;
            lv_obj_set_hidden(bar_indic_voice,true);
            lv_obj_set_hidden(bar_indic_bl,true);
        }
    }
    if (get_spk_recogni_info() == SPK_BACK)
    {
        clr_spk_recogni_info();
        lv_avi_pause(avi_2);
    	stop_harware_dac();
        lv_obj_set_hidden(avi_2, true);
         lv_obj_set_hidden(image_close, true);
        flag_start_play_avi = 0;
        lv_obj_set_click(avi_2,false);

        stat_bar_restart_flush();

        synwit_ui_load_screen(SCREEN005);       
    }

    if (flag_start_play_avi == 1)
    {
        off_time_clr();
        if (check_low_bat_trig(LOW_BAT_THRESHOLD - 10))
        {
            printf("low bat\n");
            lv_avi_pause(avi_2);
            stop_harware_dac();
            lv_obj_set_hidden(avi_2, true);
             lv_obj_set_hidden(image_close, true);
            flag_start_play_avi = 0;
            lv_obj_set_click(avi_2,false);
    
            stat_bar_restart_flush();
            g_flag_low_bat = 1;
            synwit_ui_load_screen(SCREEN013);
        }    
        if (g_s_set.voice_level > 0)
        {
            my_dac_open();
        }
        avi_state_t avi_state = lv_avi_get_state(avi_2);
        // printf("lv_avi_get_state avi_2 = %d\n",avi_state);
        if (avi_state == AVI_STATE_STOPPED)
        {
            fil_cnt++;
            if (fil_cnt > 1000 / 10)
            {
                fil_cnt = 0;
                if (lv_avi_set_src(avi_2, video_str_table2[video_index]) == 0)
                {
                    printf("lv_avi_replay\n");
                    lv_avi_replay(avi_2);
                    dac_init(DAC_FORMAT_MSB12B);
                    my_dac_open();

                }
            }
        }
        else
        {
            fil_cnt = 0;
        }

    }
    if (get_spk_recogni_info() == WATCH_VIDEO_1)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_1,LV_EVENT_CLICKED,NULL);
    }
    if (get_spk_recogni_info() == WATCH_VIDEO_2)
    {
        clr_spk_recogni_info();
        lv_event_send(touch_area_avi_2,LV_EVENT_CLICKED,NULL);
    }

    if (get_spk_recogni_info() == BL_UP || get_spk_recogni_info() == BL_DOWN)
    {
        clr_spk_recogni_info();
        if (flag_start_play_avi == 1)
        {
            lv_obj_set_hidden(bar_indic_bl,false);
            flag_disp_bar = 1;
            lv_bar_set_value(bar_indic_bl,g_s_set.bl_level * BL_LEVEL_STEP_VAL,LV_ANIM_OFF);
        }
    }
    if (get_spk_recogni_info() == VOICE_UP || get_spk_recogni_info() == VOICE_DOWN || get_spk_recogni_info() == VOICE_SHUT)
    {
        clr_spk_recogni_info();
        if (flag_start_play_avi == 1)
        {
            lv_obj_set_hidden(bar_indic_voice,false);
            flag_disp_bar = 1;
            if (g_s_set.voice_level * VOICE_LEVEL_STEP_VAL < 10)
            {
                lv_bar_set_value(bar_indic_voice,5,LV_ANIM_OFF);
            }
            else
            {
                lv_bar_set_value(bar_indic_voice,g_s_set.voice_level * VOICE_LEVEL_STEP_VAL,LV_ANIM_OFF);
            }
            lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
            if (g_s_set.voice_level == 0)
            {
                stop_harware_dac();
                g_flag_video_delay_play = 0;
            }
            else
            {
                my_dac_open();
            }
        }
    } 

}

static void screen007_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    flag_start_play_avi = 0;
    flag_disp_bar = 0;
    flag_one_stop_trig = 0;

    avi_2 = synwit_ui_find_lv_obj(WIDGET_AVI_1);
    image_close = synwit_ui_find_lv_obj(WIDGET_IMAGE11);
    lv_obj_set_click(image_close,true);
    tc_area_back = synwit_ui_find_lv_obj(WIDGET_TOUCH_18);
    bar_indic_voice = synwit_ui_find_lv_obj(WIDGET_BAR2);
    bar_indic_bl = synwit_ui_find_lv_obj(WIDGET_BAR3);

    image_video = synwit_ui_find_lv_obj(WIDGET_IMAGE10);

    touch_area_avi_1 = synwit_ui_find_lv_obj(WIDGET_TOUCH_46);
    touch_area_avi_2 = synwit_ui_find_lv_obj(WIDGET_TOUCH_47);

    lv_obj_set_hidden(avi_2, true);

    lv_bar_set_value(bar_indic_voice,g_s_set.voice_level * VOICE_LEVEL_STEP_VAL,LV_ANIM_OFF);
    lv_bar_set_value(bar_indic_bl,g_s_set.bl_level * BL_LEVEL_STEP_VAL,LV_ANIM_OFF);

    lv_obj_set_hidden(bar_indic_voice, true);
    lv_obj_set_hidden(bar_indic_bl, true);
    lv_obj_set_ext_click_area(image_close, 50, 0, 0, 50);


    lv_obj_set_event_cb(image_close, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(tc_area_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(avi_2, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(touch_area_avi_1, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(touch_area_avi_2, my_event_cb);   /*Assign an event callback*/

    check_low_bat_init();

    // lv_obj_set_event_cb(bar_indic_voice, my_event_cb);   /*Assign an event callback*/
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    screen_init();
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen007_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
    screen_stop();
    g_flag_video_delay_play = 0;
}

static int screen007_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen007_cb_obj = {
    .favorite_stop_policy = screen007_favorite_stop_policy,
    .on_screen_start = screen007_start,
    .on_screen_stop = screen007_stop,
    .on_screen_timer = screen007_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    off_time_clr();
    if (flag_start_play_avi == 0)
    {
        if (obj == touch_area_avi_1 || obj == touch_area_avi_2)
        {
            if (event == LV_EVENT_CLICKED)
            {
                // synwit_ui_load_screen(SCREEN004);
                if (obj == touch_area_avi_1)
                {
                    video_index = 0;
                }
                else if (obj == touch_area_avi_2)
                {
                    video_index = 1;
                }
                if (check_low_bat_pro(LOW_BAT_THRESHOLD) == 1)
                {
                    g_flag_low_bat = 1;
                    synwit_ui_load_screen(SCREEN013);
                    return;
                }
                
                if (lv_avi_set_src(avi_2, video_str_table2[video_index]) == 0)
                {
                    flag_start_play_avi = 1;
                    g_flag_video_delay_play = 1;
                    lv_avi_replay(avi_2);
                    lv_obj_set_hidden(avi_2, false);
                    lv_obj_set_hidden(image_close, false);
                    lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
                    if (g_s_set.voice_level == 0)
                    {
                        stop_harware_dac();
                        g_flag_video_delay_play = 0;
                    }
                    else
                    {
                        my_dac_open();
                    }
                    dac_init(DAC_FORMAT_MSB12B);
                    lv_obj_set_click(avi_2,true);
                    stat_bar_stop_flush(0);
                    lv_avi_set_on_completed_cb(avi_2,my_avi_complete_cb, NULL);
                }
    
            }   
        }
        if (obj == tc_area_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                synwit_ui_load_screen(SCREEN005);
            }
        }
    }
    else
    {
        if (obj == image_close)
        {
            if (event == LV_EVENT_CLICKED)
            {
                // synwit_ui_load_screen(SCREEN004);
                lv_avi_pause(avi_2);
                flag_one_stop_trig = 1;
                stop_harware_dac();
                lv_obj_set_hidden(avi_2, true);
                lv_obj_set_hidden(image_close, true);
                flag_start_play_avi = 0;
                g_flag_video_delay_play = 0;

                lv_obj_set_click(avi_2,false);
                lv_obj_set_hidden(bar_indic_voice,true);
                lv_obj_set_hidden(bar_indic_bl,true);

                stat_bar_restart_flush();
            }
        }
        else if (obj == avi_2)
        {
            static lv_point_t point_start;
            static lv_point_t point_end;
            if (event == LV_EVENT_PRESSED)
            {
                lv_indev_get_point(lv_indev_get_act(),&point_start);
                printf("LV_EVENT_PRESSED point_start x=%d y=%d \n",point_start.x,point_start.y);
            }
            if (event == LV_EVENT_RELEASED)
            {
                lv_indev_get_point(lv_indev_get_act(),&point_end);
                printf("LV_EVENT_RELEASED point_end x=%d y=%d \n",point_end.x,point_end.y);
                s_slide_info_t s_slide_info = get_slide_info(point_start,point_end);
                printf("slide_info.area=%d slide_info.dir=%d slide_info.strength=%d\n",s_slide_info.area,s_slide_info.dir,s_slide_info.strength);
                int16_t bar_val;
                if (s_slide_info.area == area_right)
                {
                    bar_val = lv_bar_get_value(bar_indic_voice);
                    if (s_slide_info.dir == dir_up)
                    {
                        bar_val += s_slide_info.strength * VOICE_LEVEL_STEP_VAL;
                        if (bar_val > 100)
                        {
                            bar_val = 100;
                        }
                    }
                    else
                    {
                        bar_val -= s_slide_info.strength * VOICE_LEVEL_STEP_VAL;
                        if (bar_val <= 0)
                        {
                            bar_val = 0;
                        }
                    }
                    if (bar_val <= 5)
                    {
                        lv_bar_set_value(bar_indic_voice,5,LV_ANIM_OFF);
                    }
                    else
                    {
                        lv_bar_set_value(bar_indic_voice,bar_val,LV_ANIM_OFF);
                    }
                    lv_obj_set_hidden(bar_indic_voice,false);
                    flag_disp_bar = 1;
                    g_s_set.voice_level = get_voice_level(bar_val);
                    lv_avi_set_volume(avi_2, get_video_voice_val(g_s_set.voice_level), AVI_VOLUME_ADJ_MODE_ABSOLUTE);
                    if (g_s_set.voice_level == 0)
                    {
                        stop_harware_dac();
                        g_flag_video_delay_play = 0;
                    }
                    else
                    {
                        my_dac_open();
                    }
        
                    printf("voice_level:%d\n",g_s_set.voice_level);

                }
                else if (s_slide_info.area == area_left)
                {
                    bar_val = lv_bar_get_value(bar_indic_bl);
                    if (s_slide_info.dir == dir_up)
                    {
                        bar_val += s_slide_info.strength * BL_LEVEL_STEP_VAL;
                        if (bar_val > 100)
                        {
                            bar_val = 100;
                        }
                    }
                    else
                    {
                        bar_val -= s_slide_info.strength * BL_LEVEL_STEP_VAL;
                        if (bar_val <= 0)
                        {
                            bar_val = 0;
                        }
                    }
                    if (bar_val <= 5)
                    {
                        lv_bar_set_value(bar_indic_bl,5,LV_ANIM_OFF);
                    }
                    else
                    {
                        lv_bar_set_value(bar_indic_bl,bar_val,LV_ANIM_OFF);
                    }
                    lv_obj_set_hidden(bar_indic_bl,false);
                    flag_disp_bar = 1;
                    g_s_set.bl_level = get_bl_level(bar_val);
                    set_bl_by_level(g_s_set.bl_level);
                    printf("bl_level:%d\n",g_s_set.bl_level);
                }
            }
            
        }     
    }


}

static void my_avi_complete_cb(lv_obj_t* avi, void *user_data)
{
    lv_avi_pause(avi_2);
	stop_harware_dac();
    lv_obj_set_hidden(avi_2, true);
    lv_obj_set_hidden(image_close, true);
    flag_start_play_avi = 0;
    lv_obj_set_click(avi_2,false);
    stat_bar_restart_flush();
}

